
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type {  LoginDataResult } from '@/types/login'



// 定义 Store
export const useMemberStore = defineStore(
  'user',
  () => {
    // 会员信息（直接使用响应式属性）
    const userId = ref('')
    const accessToken = ref('')
    const refreshToken = ref('')
    const expiresTime = ref('')

    const username = ref('')
    const wxps = ref('') //存权限

   
    // 保存会员信息，登录时使用
    const setProfile = (val: LoginDataResult) => {
      userId.value = val?.userId || ''
      accessToken.value = val?.accessToken || ''
      refreshToken.value = val?.refreshToken || ''
      expiresTime.value = val?.refreshToken || ''
    }

    const setPermInfo = (val: LoginDataResult) => {
      username.value = val?.username || ''
      wxps.value = val?.wxps || ''
    }

    const setToken = (token: string) => {
      accessToken.value = token || ''
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      userId.value = ''
      accessToken.value = ''
      refreshToken.value = ''
      expiresTime.value = ''
    }

    return {
      userId,
      accessToken,
      refreshToken,
      expiresTime,
      username,
      wxps,

      setProfile,
      setPermInfo,
      setToken,
      clearProfile,
    }
  },
  {
    persist: {
      storage: {
        getItem: (key) => uni.getStorageSync(key),
        setItem: (key, value) => uni.setStorageSync(key, value),
      },
    },
  }
)